package 淘宝爬虫;

import com.google.gson.Gson;
import http.Http;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import 淘宝爬虫.model.MobileModel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by User on 2017/12/26.
 */
public class APP2 {

    private static final Logger log = LoggerFactory.getLogger(APP2.class);

    private static AtomicLong count = new AtomicLong(0);

    public static void main(String[] args) throws IOException, InterruptedException {


        if (true) return;
        /*String s = Http.sendGet("http://s.taobao.com/search?q=%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B9%A6%E7%B1%8D&ssid=s5-e&&bcoffset=1&search_type=item&tab=all&sourceId=tb.index&style=list&cd=false&s=0&sort=default&ajax=true");
        System.out.println(s);*/


        /*String s1 = Http.directGet("http://s.taobao.com/search?q=%E5%85%AB%E9%87%8D%E6%8A%80%E6%9C%AF213321&ssid=s5-e&&bcoffset=1&search_type=item&tab=all&sourceId=tb.index&style=list&cd=false&s=0&sort=default&ajax=true",
                "http://s.taobao.com/", null, null);

        System.out.println(s1);


        Http.HttpURLConnectionSender httpURLConnectionSender = http.new HttpURLConnectionSender();
        String s2 = httpURLConnectionSender.sendPost("http://s.taobao.com/search?q=%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B9%A6%E7%B1%8D&ssid=s5-e&&bcoffset=1&search_type=item&tab=all&sourceId=tb.index&style=list&cd=false&s=0&sort=default&ajax=true", null, "UTF-8");

        System.out.println(s2);*/

        String content = "";
        String url;
        try {
            for (int page = 1;;page++) {
                String encodeKeyword = URLEncoder.encode("t恤女秋冬季2017新款", "UTF-8");
                System.out.println(encodeKeyword);
                url = "https://s.m.taobao.com/search?event_submit_do_new_search_auction=1&_input_charset=utf-8&topSearch=1&atype=b&searchfrom=1&action=home%3Aredirect_app_action&from=1&q="+encodeKeyword+"&sst=1&n=20&buying=buyitnow&m=api4h5&abtest=7&wlsort=7&style=list&closeModues=nav%2Cselecthot%2Conesearch&page=" + page;
                content = sendGet(url);
                Gson gson = new Gson();
                MobileModel mobileModel = gson.fromJson(content, MobileModel.class);
                List<MobileModel.ListItemBean> listItem = mobileModel.getListItem();
                for (MobileModel.ListItemBean listItemBean : listItem) {
                    String item_id = listItemBean.getItem_id();
                    System.out.print(item_id + " ");
                }
                System.out.println();
                Thread.sleep(3000);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("#####################");
            System.out.println(content);
        }


    }

    public static String sendGet(String url) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpGet httpGet = new HttpGet(url);

        Header[] headers = createHeaders();
        httpGet.setHeaders(headers);

        StringBuilder stringBuilder = new StringBuilder();
        try (CloseableHttpResponse execute = client.execute(httpGet)) {
            HttpEntity entity = execute.getEntity();
            InputStream content = entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();


    }

    public static String post(int retry, String url) throws InterruptedException {
        if (retry < 0) return null;

        Http http = new Http();
        Http.HttpClientSender httpClientSender = http.new HttpClientSender();
        String s = Http.directGet(url, null, null, null);
        if ("".equals(s.trim())) {
            Thread.sleep(500);
            return post(--retry, url);
        }

        return s;
    }

    public static Header[] createHeaders() {
        List headersList = new ArrayList();
        headersList.add(new BasicHeader(":authority:", "s.m.taobao.com"));
        headersList.add(new BasicHeader(":method", "GET"));
        headersList.add(new BasicHeader(":path", "/search?q=t%E6%81%A4%E5%A5%B3%E7%A7%8B%E5%86%AC%E5%AD%A32017%E6%96%B0%E6%AC%BE&search=%E6%8F%90%E4%BA%A4&tab=all&sst=1&n=20&buying=buyitnow&m=api4h5&abtest=1&wlsort=1&style=list&closeModues=nav%2Cselecthot%2Conesearch&page=2"));
        headersList.add(new BasicHeader(":scheme", "https"));
        headersList.add(new BasicHeader("accept", "application/json"));
        headersList.add(new BasicHeader("accept-encoding", "gzip, deflate, sdch"));
        headersList.add(new BasicHeader("accept-language", "zh-CN,zh;q=0.8"));
        headersList.add(new BasicHeader("cookie", "UM_distinctid=15d7320e2568dd-02c4fabae-1171446e-1fa400-15d7320e257b43; ali_ab=183.156.76.165.1500875106097.3; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; l=AsjIoqQsMmP-gHNBqmweJsUfEDzasSx7; tg=0; miid=577002951166262834; mt=ci=-1_0; cookie2=2bfc3739ab1507fe91ba55debe17febd; v=0; _tb_token_=356371eaf1138; cna=McfgEe2xJQ0CAXPHZhZ17imY; _m_h5_tk=dc1229df3e493b64aeaa021390e98c81_1514378326757; _m_h5_tk_enc=87c51d17a455c0f77b19264d1cd7f9eb; ockeqeudmj=vpssIDw%3D; munb=2161592039; WAPFDFDTGFG=%2B4cMKKP%2B8PI%2BuiqiESDR39kofbo%3D; _w_app_lg=18; unb=2161592039; sg=%E5%8A%9B94; t=d725f3fdd49c9cdb524b91bb6f05fd17; _l_g_=Ug%3D%3D; skt=f5c2a1a15e022630; uc1=cookie21=W5iHLLyFeYZ1WM9hVnmS&cookie15=VT5L2FSpMGV7TQ%3D%3D&cookie14=UoTdf1JN9JRPzQ%3D%3D; cookie1=W8g1%2FHiywhd%2FdGxUrKj880CUpa%2BZGrIsxWEkumlUz8g%3D; uc3=vt3=F8dBzLba501ZXpx84i0%3D&id2=UUkIFCnHHMGLcg%3D%3D&nk2=tacSqkR9&lg2=Vq8l%2BKCLz3%2F65A%3D%3D&sg2=W5jAXkfdH6pxrEPI03UtePQShqjScSVvnBgYbDnKlDg%3D; tracknick=%5Cu6731%5Cu4E7E%5Cu529B; uss=VWeZVXB6yJqdQr5P7wjXOOqtX4mpzAiXlnS%2Bq5LCtkS22UEvuffHDbA%3D; lgc=%5Cu6731%5Cu4E7E%5Cu529B; _cc_=Vq8l%2BKCLiw%3D%3D; _nk_=%5Cu6731%5Cu4E7E%5Cu529B; cookie17=UUkIFCnHHMGLcg%3D%3D; ntm=0; JSESSIONID=D4F725680ADDAE366A7CD56E2FE35E5C; isg=Anl5ELJHBytPF9iwltwRyDIAhuWTLmPljGh8vpuuIqBPIpi049avCGc01Bwr"));
        headersList.add(new BasicHeader("referer", "https://s.m.taobao.com/h5?q=t%E6%81%A4%E5%A5%B3%E7%A7%8B%E5%86%AC%E5%AD%A32017%E6%96%B0%E6%AC%BE&search=%E6%8F%90%E4%BA%A4&tab=all"));
        headersList.add(new BasicHeader("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36"));
        headersList.add(new BasicHeader("x-requested-with", "XMLHttpRequest"));

        return (Header[]) headersList.toArray(new Header[headersList.size()]);
    }
}
